import 'package:client_flutter_door/bean/cabinet.dart';
import 'package:client_flutter_door/model/door_model.dart';
import 'package:client_flutter_door/widgets/box.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class Panel extends StatefulWidget {
  const Panel({super.key});

  @override
  State<Panel> createState() => _PanelState();
}

class _PanelState extends State<Panel> {
  late List<Widget> genalRows = [];

  Future<Widget> loadBox(BuildContext context) async {
    genalRows = [];
    List<Cabinet> boxs = Provider.of<DoorModel>(context).boxs;
    
    // print(boxs.length);
    
      int count = 0;
      int colsCount = (boxs.length / 7).ceil();

      for (int col = 0; col < colsCount; col++) {
        List<Widget> genalCols = [];
        for (int row = 0; row < 7; row++) {
          count++;
          if (count == 23 || count == 24) {
            genalCols.add(Box(index: count, czh: '', occupied: false,is400AF: false,));
          } else {
            if (count > 23) {
              if (count - 3 < boxs.length) {
                genalCols.add(Box(
                    index: count,
                    czh: boxs[count - 3].czh, //去掉安卓机占用的两个柜子
                    occupied: boxs[count - 3].occupied,
                    is400AF:boxs[count - 3].is400AF
                ));
              }
            } else {
              genalCols.add(Box(
                  index: count,
                  czh: boxs[count - 1].czh,
                  occupied: boxs[count - 1].occupied,
                  is400AF:boxs[count - 1].is400AF
              ));
            }
          }
        }
        genalRows.add(Column(
          children: genalCols,
        ));
      }
    
    return Scrollbar(
      child: SingleChildScrollView(
        primary: true,
        scrollDirection: Axis.horizontal,
        child: Column(children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: genalRows,
          ),
          Center(
            child: Row(children: [
              IconButton(
                  tooltip: '腰包已领取',
                  onPressed: () {},
                  icon: const Icon(
                    Icons.local_offer,
                    color: Colors.red,
                  )),
              const Text('腰包已领取'),
              IconButton(
                  tooltip: '腰包未领取',
                  onPressed: () {},
                  icon: const Icon(
                    Icons.local_offer,
                    color: Colors.green,
                  )),
              const Text('腰包未领取'),
              IconButton(
                  tooltip: '空柜',
                  onPressed: () {},
                  icon: const Icon(
                    Icons.local_offer,
                    color: Colors.grey,
                  )),
              const Text('空柜'),
            ]),
          )
        ]),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // return Wrap(
    //   children: widget.boxs,
    // );
    return FutureBuilder(
      future: loadBox(context),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const CircularProgressIndicator();
        }
        if (snapshot.connectionState == ConnectionState.done) {
          if (snapshot.hasError) {
            return const Text('Error');
          }
        }
        return snapshot.data!;
      },
    );
  }
}
